package geometry.algorithms
{
	import geometry.Figure;
	import geometry.IGeobject;
	import geometry.Polyline;
	import geometry.util.Vector2d;
	
	public class GeobjectClassifier
	{
		// --------------------------------------------------------------------------------------------
		public function GeobjectClassifier()
		{
		}

		// --------------------------------------------------------------------------------------------
		public function classify(points : Array) : IGeobject{
			var dpps : DPPolylineSimplification = new DPPolylineSimplification();
			var polyline : Polyline;;
			
			polyline = new Polyline( dpps.simplify(points) );

			if( isClosed(polyline) ){
				return new Figure(polyline.points);
			}
			
			return polyline;			
		}
		
	  // --------------------------------------------------------------------------------------------
	  /**
	  * Polyline is closed if distance from first to last point is less then
	  * some constant % from whole length
	  */
		private function isClosed(polyline : Polyline):Boolean{
			
			var tolerance : Number = 0.05 * polyline.length;
			var v : Vector2d;
			var points : Array = polyline.points;
			
			if(points.length < 2)
				return false;
				
			v = Vector2d.points2Vector(points[0], points[points.length-1]);
			
			return (v.length < tolerance);
		}
		
		// --------------------------------------------------------------------------------------------
		// Private members
	}
}